﻿using System;
using System.Configuration;
using System.Web.Mvc;
using Common;
using Controllers.Extend;
using IBll;
using Model.DB;
using Model.Sys.Config;
using Model.Sys.Constant;
using ViewModel;

namespace Controllers.Controllers
{
    //[Exception]
    public class LoginController : UserBaseController
    {
        protected override void OnAuthorization(AuthorizationContext filterContext)
        {
            base.cookieName = CookieConfig.UserInfoLoginCookieName;
            base.OnAuthorization(filterContext);
            if (loginUser != null)
            {
                filterContext.Result = new RedirectResult("/UserInfo/Home");
            }
        }

        [HttpGet]
        public ActionResult Index()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Index(string validateCode, string username, string password)
        {
            validateCode = validateCode.ToLower().Trim();
            CookieConfig cookieConfig = new CookieConfig();

            //验证
            if (CryptoHelper.MD5Encrypt(validateCode) == CookieHelper.GetCookieValue(CookieConfig.ValidateCodeCookieName))
            {
                UserInfo userInfo = CurrentBllSession.UserInfoBll.GetUserInfoByUsernamePassword(username, password);
                if (userInfo == null)
                {
                    return Json(new ResultModel { Msg = "用户不存在", Pass = false });
                }
                else
                {
                    string cookieValue = Constant.CacheKey.UserInfoLoginCacheKey + "_" + StringHelper.GetGuidString();
                    CookieHelper.SetCookie(CookieConfig.UserInfoLoginCookieName, cookieValue);
                    //更新用户登录信息
                    userInfo.LastLoginTime = DateTime.Now;
                    userInfo.LastLoginIp = HttpHelper.GetClientIPAddress();
                    CurrentBllSession.UserInfoBll.Update(userInfo);
                    //加入缓存
                    DateTime expireTime = DateTime.Now.AddMinutes(Double.Parse(CacheConfig.CacheTime));
                    CurrentBllSession.CacheBll.Add(cookieValue, userInfo, expireTime);
                    //清除验证码code
                    CookieHelper.ClearCookie(CookieConfig.ValidateCodeCookieName);
                    return Json(new ResultModel { Msg = "登陆成功", Pass = true });
                }
            }
            else
            {
                return Json(new ResultModel { Msg = "验证码错误", Pass = false });
            }

            return View();
        }

        [HttpGet]
        public void ValidateCode()
        {
            string validateCode = "";
            string validateCodeEncrypt = "";

            Common.ValidateCode.CreateValidateGraphic(out validateCode, 4, 140, 40, 24);
            validateCodeEncrypt = CryptoHelper.MD5Encrypt(validateCode);//加密
            CookieHelper.SetCookie(CookieConfig.ValidateCodeCookieName, validateCodeEncrypt);
        }

    }
}
